<?php

namespace Model\Room;

use Model\User;
use Handler\TPHandler;

class Player {
	/**
	 *
	 * @var RoomMain
	 */
	private $m_RoomMain;
	/**
	 *
	 * @var User
	 */
	private $m_User;
	/**
	 * 是否在线
	 *
	 * @var boolean
	 */
	private $m_IsOnline;
	/**
	 * 座位号
	 *
	 * @var unknown
	 */
	private $m_SeatId;
	/**
	 * 是否准备
	 *
	 * @var unknown
	 */
	public $m_IsReady;
	/**
	 * 是否开始游戏
	 *
	 * @var boolean
	 */
	public $m_IsPlaying;
	/**
	 * 是否已抢庄
	 *
	 * @var boolean
	 */
	public $m_IsQiangZhuang;
	/**
	 * 是否为庄家
	 *
	 * @var boolean
	 */
	public $m_IsBanker;
	/**
	 * 是否已下注
	 *
	 * @var boolean
	 */
	public $m_IsBet;
	/**
	 * 是否已开牌
	 *
	 * @var boolean
	 */
	public $m_IsOpen;
	/**
	 * 庄家倍数(0,1,2,4)当所有玩家都不抢的时候，随机一个，庄家倍数设1
	 *
	 * @var int
	 */
	public $m_BankerMul;
	/**
	 * 下注倍数（5，10，15，20）
	 *
	 * @var int
	 */
	public $m_BetMul;
	/**
	 * 牌型
	 *
	 * @var int
	 */
	public $m_CardType;
	/**
	 * 手牌
	 *
	 * @var string[]
	 */
	public $m_Pokers;
	/**
	 *
	 * @var string[]
	 */
	public $m_CleanUpPokers;
	/**
	 * 当前回合分数
	 *
	 * @var unknown
	 */
	public $m_RoundPoint;
	/**
	 *
	 * @var int
	 */
	public $m_CurTime;
	/**
	 * 是否受控ID
	 *
	 * @var unknown
	 */
	public $m_IsControl;
	/**
	 *
	 * @var unknown
	 */
	public $m_ControlDifficulty;
	
	public $m_PresetPokers;
	/**
	 *
	 * @param User $user        	
	 */
	function __construct($id, $user, $rMain) {
		$this->m_SeatId = $id;
		$this->m_RoomMain = $rMain;
		$this->m_User = $user;
		$this->m_IsOnline = true;
		
		if ($this->m_RoomMain->m_ControlId == $user->m_Id) {
			$this->m_IsControl = true;
			$this->m_ControlDifficulty = $this->m_RoomMain->m_ControlDifficulty;
		} else {
			$this->m_IsControl = false;
			$this->m_ControlDifficulty = RoomMain::DIFF_NORMAL;
		}
		
		$this->m_IsReady = false;
		
		// if ($this->m_User->m_IsRobot)
		// $this->m_IsReady = true;
		// else
		// $this->m_IsReady = false;
		$this->m_PresetPokers = array();
		$this->mb_ReStart ();
		
		$this->mb_Start ();
	}
	/**
	 */
	function __destruct() {
	}
	/**
	 * 重连
	 *
	 * @param unknown $user        	
	 */
	public function mb_ReConnect($user) {
		$this->m_User = $user;
		$this->m_IsOnline = true;
	}
	/**
	 * 断线
	 */
	public function mb_DisConnect() {
		// $this->m_User->mb_Disconnect ();
		$this->m_IsOnline = false;
	}
	/**
	 * 重新开始
	 */
	public function mb_ReStart() {
		$this->m_IsPlaying = false;
		$this->m_IsQiangZhuang = false;
		$this->m_IsBanker = false;
		$this->m_BankerMul = 0;
		$this->m_IsBet = false;
		$this->m_IsOpen = false;
		$this->m_BetMul = 0;
		$this->m_Pokers = array ();
		$this->m_CleanUpPokers = array ();
		$this->m_CardType = RoomMain::CARDTYPE_MEINIU;
		$this->m_RoundPoint = 0;
	}
	/**
	 * 获取同步信息
	 *
	 * @return string[]
	 */
	public function mb_GetSyncData() {
		return array (
				'seatid' => $this->m_SeatId,
				'name' => $this->m_User->m_Name,
				'nick' => $this->m_User->m_Nick,
				'coin' => ( double ) $this->m_User->m_Coin,
				'isready' => $this->m_IsReady,
				'isplaying' => $this->m_IsPlaying,
				'isqiangzhuang' => $this->m_IsQiangZhuang,
				'bankermul' => ( int ) $this->m_BankerMul,
				'isbanker' => $this->m_IsBanker,
				'isbet' => $this->m_IsBet,
				'isopen' => $this->m_IsOpen,
				'betmul' => ( int ) $this->m_BetMul,
				'pokers' => $this->m_Pokers,
				'cleanuppokers' => $this->m_CleanUpPokers,
				'cardtype' => $this->m_CardType,
				'round' => ( double ) $this->m_RoundPoint 
		);
	}
	/**
	 *
	 * @param boolean $bIsControl        	
	 */
	public function mb_UpdateControlInfo() {
		if ($this->m_RoomMain->m_ControlId == $this->m_User->m_Id) {
			$this->m_IsControl = true;
			$this->m_ControlDifficulty = $this->m_RoomMain->m_ControlDifficulty;
		} else {
			$this->m_IsControl = false;
			$this->m_ControlDifficulty = RoomMain::DIFF_NORMAL;
		}
	}
	// ------------------------------------------------------------------//
	/**
	 * 抢庄
	 *
	 * @param int $bankerMul        	
	 */
	public function mb_QiangZhuang($bankerMul) {
		$this->m_IsQiangZhuang = true;
		$this->m_BankerMul = $bankerMul;
	}
	/**
	 * 保存数据
	 */
	public function mb_SaveData() {
		if ($this->m_IsPlaying) {
			$this->m_User->mb_Save ( $this->m_RoundPoint );
			if ($this->m_User->m_IsRobot == false) {
				TPHandler::mb_GetInstance ()->mb_Save ( $this->m_User->m_Name, round ( $this->m_RoundPoint / $this->m_User->m_CoinValue, 2 ), '' );
			}
		}
	}
	// ------------------------------------------------------------------//
	private function mb_Start() {
	}
	public function mb_Update() {
		switch ($this->m_RoomMain->m_CurStatus) {
			case RoomMain::STATUS_WAIT :
				$this->mb_UpdateWait ();
				break;
			case RoomMain::STATUS_QIANGZHUANG :
				$this->mb_UpdateQiangZhuang ();
				break;
			case RoomMain::STATUS_DOWNBET :
				$this->mb_UpdateDownBet ();
				break;
			case RoomMain::STATUS_OPEN :
				$this->mb_UpdateOpenPoker ();
				break;
			case RoomMain::STATUS_OVER :
				break;
			default :
				break;
		}
	}
	public function mb_EnterWait() {
	}
	private function mb_UpdateWait() {
		if ($this->m_IsReady == false) {
			if ($this->m_RoomMain->m_Algo->mb_GetRealPlayerCount () > 0) {
				if (mt_rand ( 0, 100 ) > 90) {
					$this->m_IsReady = true;
				}
			}
			// if ($this->m_RoomMain->m_Algo->mb_GetRealPlayerCount () == 1 && $this->m_RoomMain->m_Algo->mb_GetPlayerCount () == 1) {
			// if (mt_rand ( 0, 100 ) > 90) {
			// $this->m_IsReady = true;
			// }
			// } else {
			// if (mt_rand ( 0, 100 ) > 90) {
			// $this->m_IsReady = true;
			// }
			// }
		}
	}
	public function mb_EnterQiangZhuang() {
		$this->m_CurTime = mt_rand ( 2, 4 );
	}
	private function mb_UpdateQiangZhuang() {
		if ($this->m_IsPlaying == false)
			return;
		if (-- $this->m_CurTime == 0) {
			$bankerMuls = array (
					0,
					1,
					2,
					4 
			);
			$mulIndex = mt_rand ( 0, 3 );
			$this->m_RoomMain->m_Algo->mb_QiangZhuang ( $this->m_User, $bankerMuls [$mulIndex] );
		}
	}
	public function mb_EnterDownBet() {
		$this->m_CurTime = mt_rand ( 2, 4 );
	}
	private function mb_UpdateDownBet() {
		if ($this->m_IsPlaying == false)
			return;
		if (-- $this->m_CurTime == 0 && $this->m_IsBanker == false) {
			$betMul = array (
					5,
					10,
					15,
					20 
			);
			$mulIndex = mt_rand ( 0, 3 );
			$this->m_RoomMain->m_Algo->mb_DownBet ( $this->m_User, $betMul [$mulIndex] );
		}
	}
	public function mb_EnterOpenPoker() {
		$this->m_CurTime = mt_rand ( 1, 4 );
	}
	private function mb_UpdateOpenPoker() {
		if ($this->m_IsPlaying == false)
			return;
		if (-- $this->m_CurTime == 0) {
			$this->m_RoomMain->m_Algo->mb_OpenPoker ( $this->m_User );
		}
	}
	// ------------------------------------------------------------------//
	public function __get($property_name) {
		if (isset ( $this->$property_name )) {
			return ($this->$property_name);
		} else {
			return (NULL);
		}
	}
}